package com.project.restaurant.user.controller;

import com.project.restaurant.common.api.ResponseResult;
import com.project.restaurant.common.api.ServiceReturn;
import com.project.restaurant.common.api.ServiceStateCode;
import com.project.restaurant.common.key.SessionAttribute;
import com.project.restaurant.pojo.User;
import com.project.restaurant.user.dto.UpdateEmailParam;
import com.project.restaurant.user.dto.UpdateUserInfoParam;
import com.project.restaurant.user.service.UserInfoService;
import com.project.restaurant.user.vo.UserPublicInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import org.apache.http.protocol.HTTP;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;
import java.io.IOException;

@Api(value = "用户信息接口",tags = "用户信息接口")
@RestController
@RequestMapping("/user/info")
public class UserInfoController {

    @Resource
    private UserInfoService userInfoService;


    @ApiOperation(value = "更换头像",notes = "返回更换头像后的头像链接")
    @ApiResponse(code = 200, message = "操作成功", response = String.class)
    @PostMapping(value = "/update/avatar")
    public ResponseResult<String> updateAvatar(@ApiIgnore HttpSession session,
                                               @ApiParam(value = "变更的信息", required = true) @RequestBody MultipartFile avatar) throws Exception {

        if (avatar==null){
            return ResponseResult.failure(ServiceStateCode.PARAMETER_NONSTANDARD);
        }
        String result = userInfoService.updateAvatar(session,avatar);
        return  ResponseResult.success(result);
    }

    @ApiOperation(value = "修改用户信息",notes = "返回修改后的用户信息")
    @ApiResponse(code = 200, message = "操作成功", response = UserPublicInfoVo.class)
    @PostMapping("/update/info")
    public ResponseResult<UserPublicInfoVo> updateInfo(@ApiIgnore HttpSession session,
                                                       @ApiParam(value = "变更的信息", required = true) @RequestBody @Validated UpdateUserInfoParam updateInfo){
        UserPublicInfoVo result = userInfoService.updateInfo(updateInfo);
        return ResponseResult.success(result);
    }

////    @ApiOperation(value = "更换邮箱",notes = "返回更换后的邮箱")
////    @ApiResponse(code = 200, message = "操作成功", response = String.class)
//    @PostMapping("/update/email")
//    public ResponseResult<String> updateEmail(@ApiIgnore HttpSession session,
//                                              @ApiParam(value = "变更的信息", required = true) @RequestBody @Validated UpdateEmailParam updateEmail){
//        return null;
//    }
//
////    @ApiOperation(value = "发送更换邮箱验证码",notes = "返回发送状态")
////    @ApiResponse(code = 200, message = "操作成功", response = Boolean.class)
//    @GetMapping("/update/email/send/{email}")
//    public ResponseResult<Boolean> sendUpdateEmailAuthCode(@ApiParam(value = "目标邮箱", required = true)  @PathVariable("email") String email){
//        return null;
//    }


    @ApiOperation(value = "获取登录用户信息")
    @ApiResponse(code = 200, message = "操作成功", response = UserPublicInfoVo.class)
    @GetMapping("/get/info")
    public ResponseResult<UserPublicInfoVo> getUserInfo(@ApiIgnore HttpSession session){
        User user = (User) session.getAttribute(SessionAttribute.NOW_LOGIN_USER);
        return ResponseResult.success(UserPublicInfoVo.getUserSafeInfoVo(user));
    }


}
